package com.yeyaomai.dksns.flowis.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yeyaomai.dksns.flowis.domain.FlowUser;
import com.yeyaomai.dksns.flowis.service.FlowAuthService;
import com.yeyaomai.dksns.flowis.service.FlowUserService;
import com.yeyaomai.dksns.flowis.util.ImageTool;
import com.yeyaomai.dksns.flowis.util.VelocityToolDateUtils;

@Controller
public class LoginController {

	@Autowired
	private FlowUserService flowUserService;

	@Autowired
	private FlowAuthService flowAuthService;

//	@RequestMapping("/")
//	public ModelAndView toss(HttpServletRequest request) {
////		request.setAttribute("url", "/toLogin");
////		System.out.println("go index");
////		return new ModelAndView("redirect:/trend/thirtydays");
//		return new ModelAndView("redirect:/trend/today?time="+VelocityToolDateUtils.getDay(0));
//	}
	
	@RequestMapping("/toLogin")
	public String toLogin(HttpServletRequest request) {

		request.setAttribute("url", "/toLogin");
		return "login";
	}

	@RequestMapping("/makeRandCode")
	public void makeRandCode(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		ImageTool.service(request, response);
	}

	@RequestMapping("/loginCheck")
	public String loginCheck(HttpServletRequest request,
			HttpServletResponse response, FlowUser user) {

		HttpSession session = request.getSession(true);

		String checkcode = request.getParameter("verifyCode");
		String code = (String) session.getAttribute("randCode");
		request.setAttribute("url", "/toLogin");

		if (checkcode != null) {
			StringBuffer str1 = new StringBuffer(checkcode);

			for (int index = 0; index < str1.length(); index++) {
				char cha = str1.charAt(index);
				if ((cha > 'a' - 1) && (cha < 'z' + 1)) {
					str1.setCharAt(index, Character.toUpperCase(cha));
				}
			}
			checkcode = str1.toString();
		}

		if (checkcode != null && code != null && code.equals(checkcode)) {
			if (user != null) {
				user.setPassword(FlowUserController.MD5(user.getPassword()));
				user = flowUserService.loginCheck(user);

				if (user == null) {
					request.setAttribute("error", 1);
				} else {
					if (user.getStatus() == 1) {
						request.setAttribute("error", 3);
					} else {
						session.removeAttribute("flowUser");
						session.setAttribute("flowUser", user);
						session.setMaxInactiveInterval(43200);
						// 查询权限放入session
						Map<Integer, String> map = new HashMap<Integer, String>();
						map = flowAuthService.getFlowAuthByRole(user
								.getRoleId(),user.getUserName());
						session.setAttribute("authMap", map);
						return "redirect:/trend/today?time="+ VelocityToolDateUtils.getDay(0);
					}
				}
			} else {
				request.setAttribute("error", 1);
			}
		} else {
			request.setAttribute("error", 2);
		}
		return "login";
	}
	
	@RequestMapping("/guest")
	public String guest(HttpServletRequest request,HttpServletResponse response){
		
		FlowUser user = flowUserService.getFlowUserByUserName("guest");
		if(user!=null) {
			HttpSession session = request.getSession(true);
			session.removeAttribute("flowUser");
			session.setAttribute("flowUser", user);
			session.setMaxInactiveInterval(43200);
			// 查询权限放入session
			Map<Integer, String> map = new HashMap<Integer, String>();
			map = flowAuthService.getFlowAuthByRole(user
					.getRoleId(),user.getUserName());
			session.setAttribute("authMap", map);
			return "redirect:/trend/today?time="+ VelocityToolDateUtils.getDay(0);
		}
		return "login";
	}

	@RequestMapping("/quit")
	public String quit(HttpServletRequest request,
			HttpServletResponse response, FlowUser user) {

		HttpSession session = request.getSession();
		session.invalidate();
		return "login";
	}
}
